const md5 = require('utility').md5;

const error = require("./error");
const rtData = (res, code, data = {}, other = {}) => {
    let result = {
        code,
        msg: error[code],
        info: data,
        other: other
    };

    res.json(result);
}

const pwdFormat = (pwd = '') => {
    const SALF = 'Oasis_2018';
    return md5(md5(SALF + pwd));
}

const delSession = (req = {}) => {
    delete req.session.userInfo;
};

const auth = (req, res) => {
    let rt = false;
    if(!req.session.userInfo){
        delSession(req);
        rtData(res, 1004);
    }else{
        rt = true;
    }
    return rt;
};

module.exports = {
    rtData,
    pwdFormat,
    delSession,
    auth
}